package com.bwie.system.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * @Author：作者姓名
 * @Package：com.bwie.system.consumer
 * @Project：_yk
 * @name：PriConsumer
 * @Date：2024/5/17 18:31
 */
@Component
@Log4j2
public class PriConsumer {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @RabbitListener(queuesToDeclare = @Queue("pri"))
    public void Pri(String userName, Message message, Channel channel){
        String messageId = message.getMessageProperties().getMessageId();
        Long login = redisTemplate.opsForSet().add("key", messageId);
        try {
            if(login==1){
                log.info("优惠券发放成功！");
            }
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (IOException e) {
            try {
                redisTemplate.opsForSet().remove("key");
                channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        }

    }



}
